package jpao.benchmark;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

/**
 *
 * @author soa
 */
public class TpsMonitor {
    
    private AtomicLong count = new AtomicLong();
    private AtomicLong errorCount = new AtomicLong();
    private long previousCount = 0L;
    private ScheduledExecutorService service = null;
    private DescriptiveStatistics statistics = new DescriptiveStatistics();
    
    public TpsMonitor(final String threadName) {
        service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            
            public Thread newThread(Runnable r) {
                return new Thread(r, threadName);
            }
        });
    }
    
    public TpsMonitor() {
        this("tps-monitor");
    }
    
    public void start() {
        service.scheduleAtFixedRate(new Runnable() {
            
            public void run() {
                long currentCount = count.get();
                long delta = currentCount - previousCount;
                previousCount = currentCount;
                statistics.addValue(delta);
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);
    }
    
    public void increment() {
        count.incrementAndGet();
    }
    
    public void incrementErrorCount() {
        errorCount.incrementAndGet();
    }
    
    public void stop() {
        service.shutdown();
    }
    
    public DescriptiveStatistics getStatistics() {
        return statistics;
    }
    
    public String getGoogleChartUrl() {

        // to determine the correct scale for axis
        double minTps = statistics.getMin() * 0.9;
        double maxTps = statistics.getMax() * 1.1;
        double minSec = 0;
        double maxSec = statistics.getN();

        // build the url (design with http://imagecharteditor.appspot.com/)
        StringBuilder sb = new StringBuilder("http://chart.apis.google.com/chart?chxl=1:||Elapsed+Time+(s)|3:||TPS&chxr=0,");
        sb.append(minTps).append(',').append(maxTps);
        sb.append("|1,");
        sb.append(minSec).append(',').append(maxSec);
        sb.append("|2,");
        sb.append(minSec).append(',').append(maxSec);
        sb.append("|3,");
        sb.append(minTps).append(',').append(maxTps);
        sb.append("&chxt=y,x,x,y&chs=440x220&cht=lxy&chds=");
        sb.append(minSec).append(',').append(maxSec);
        sb.append(',').append(minTps).append(',').append(maxTps);
        sb.append("&chd=t:");
        for (int i = 0; i < statistics.getN(); i++) {
            sb.append(i);
            if (i < statistics.getN() - 1) {
                sb.append(',');
            }
        }
        sb.append('|');
        for (int i = 0; i < statistics.getN(); i++) {
            sb.append(statistics.getElement(i));
            if (i < statistics.getN() - 1) {
                sb.append(',');
            }
        }
        sb.append("&chdlp=b&chls=2&chma=5,5,5,25&chm=B,E0EBFF,0,0,0&chtt=Performance");
        
        return sb.toString();
    }
}
